<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="130px" :model="form" :rules="rules">
      <el-form-item label="注册域名" prop="Domain">
        <el-input v-model="form.Domain" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="域名类型" prop="DomainType">
        <!-- <el-input v-model="form.DomainType" /> -->
        <el-select v-model="form.DomainType" :disabled="disabled">
          <el-option label="英文国际" value="英文国际" />
          <el-option label="英文国内" value="英文国内" />
          <el-option label="中文国际" value="中文国际" />
          <el-option label="中文国内" value="中文国内" />
        </el-select>
      </el-form-item>
      <el-form-item label="注册商" prop="Supplier">
        <!-- <el-input v-model="form.Supplier" /> -->
        <el-select v-model="form.Supplier" :disabled="disabled">
          <el-option label="西部数码" value="西部数码" />
          <el-option label="中国频道" value="中国频道" />
          <el-option label="阿里云" value="阿里云" />
          <el-option label="商务中国" value="商务中国" />
          <el-option label="新网" value="新网" />
        </el-select>
      </el-form-item>
      <el-form-item label="管理地址" prop="ManageUrl">
        <el-input v-model="form.ManageUrl" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理账户" prop="ManageAccount">
        <el-input v-model="form.ManageAccount" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="管理密码" prop="ManagePassword">
        <el-input v-model="form.ManagePassword" :disabled="disabled" />
      </el-form-item>
      <!-- <el-form-item v-if="!disabled" label="实际服务期限">
        <el-date-picker
          v-model="form.ServiceDates"
          type="daterange"
          start-placeholder="Start date"
          end-placeholder="End date"
          :default-time="defaultTime"
          :disabled="disabled"
        />
      </el-form-item> -->
      <el-form-item v-if="form.isShowPassword" label="查看密码">
        <el-input v-model="form.ShowPassword" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { saveDomain, showDomain } from '@/api/website'

  export default defineComponent({
    name: 'Domain',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        disabled: false,
        formRef: null,
        form: {},
        storeForm: {},
        rules: {
          DomainType: [
            { required: true, trigger: 'blur', message: '请选择域名类型' },
          ],
          Supplier: [{ required: true, trigger: 'blur', message: '注册商' }],
          Domain: [{ required: true, trigger: 'blur', message: '域名' }],
          ManageUrl: [{ required: true, trigger: 'blur', message: '管理地址' }],
          ManageAccount: [
            { required: true, trigger: 'blur', message: '管理账号' },
          ],
          ManagePassword: [
            { required: true, trigger: 'blur', message: '管理密码' },
          ],
          ServiceDates: [
            { required: true, trigger: 'blur', message: '请选择服务期限' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        StoreOptions: [],
        BindName: '',
      })

      const showEdit = async (row, flag, disabled) => {
        state.disabled = disabled
        console.log(row, '域名')
        state.title = row.ProductName
        state.storeForm = Object.assign({}, row)
        state.form.CustomerID = state.storeForm.CustomerID
        state.form.OrderID = state.storeForm.OrderID
        state.form.OrderListID = state.storeForm.OrderListID
        state.form.BindStoreID = state.storeForm.BindStoreID
        state.form.DomainID = row.BindID

        if (flag) {
          await gitShowDomain()
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await saveDomain(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            // fetchData()
            close()
          }
        })
      }

      const gitShowDomain = async () => {
        const dataMsg = {
          DomainID: state.form.DomainID,
        }
        const { data } = await showDomain(dataMsg)
        state.form = data
        state.form.ServiceDates = [
          state.storeForm.ServiceStartDate,
          state.storeForm.ServiceEndDate,
        ]
        state.form.CustomerID = state.storeForm.CustomerID
        state.form.OrderID = state.storeForm.OrderID
        state.form.OrderListID = state.storeForm.OrderListID
        state.form.BindStoreID = state.storeForm.BindStoreID
        state.form.DomainID = state.storeForm.BindID
        console.log(state.form, '1111')
      }
      const getData = async () => {
        state.listLoading = true

        const $params = {
          CustomerID: state.form.CustomerID,
          ProductID: state.form.ProductID,
        }
        const {
          data: { StoreOptions },
        } = await StoreSelect($params)
        state.StoreOptions = StoreOptions
        state.listLoading = false
      }

      return {
        ...toRefs(state),
        showEdit,
        gitShowDomain,
        getData,
        close,
        save,
      }
    },
  })
</script>
